package m.fun.service;

import m.base.model.UserInfo;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryPage;
import m.common.service.Service;
import m.fun.model.GoodsOffer;
import m.fun.model.OfferDetail;
import m.fun.model.OfferInfo;
import m.store.model.StoreGoods;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.NumberUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OfferDetailService extends Service {
    public void addGoodsOffer(String offerOid, List<GoodsOffer> goodsList) throws MException, SQLException {
        if(goodsList.isEmpty()) throw new MException(this.getClass(),"请选择要添加的商品报价");
        OfferInfo offer=new OfferInfo();
        offer.setOid(offerOid);
        List<OfferDetail> ls=new ArrayList<>();
        for(GoodsOffer goods : goodsList){
            if(ModelQueryList.getModelList(OfferDetail.class,new String[]{"oid"},new QueryPage(0,1),
                QueryCondition.and(
                    QueryCondition.eq("offer.oid",offerOid),
                    QueryCondition.eq("goodsOffer.oid",goods.getOid())
                )
            ).isEmpty()){
                OfferDetail model=new OfferDetail();
                model.setOid(GenerateID.generatePrimaryKey());
                model.setOffer(offer);
                model.setGoodsOffer(goods);
                ls.add(model);
            }
        }
        if(!ls.isEmpty()) ModelUpdateUtil.insertModels(ls.toArray(new OfferDetail[0]));
        getService(OfferInfoService.class).updateDetailNum(offerOid);
    }
    public void delete(String oid) throws Exception {
        OfferDetail model=ModelQueryList.getModel(OfferDetail.class,oid,"oid","offer.oid");
        if(null!=model){
            ModelUpdateUtil.deleteModel(model);
            getService(OfferInfoService.class).updateDetailNum(model.getOffer().getOid());
        }
    }
}
